# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=ADIOS2
pkgbase=mingw-w64-${_realname,,}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname,,}")
pkgver=2.10.2
pkgrel=1
pkgdesc="The Adaptable Input/Output System version 2 (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://adios2.readthedocs.io/en/latest/"
msys2_repository_url="https://github.com/ornladios/ADIOS2/"
msys2_references=(
  'archlinux: adios2'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-blosc2"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsodium"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-yaml-cpp"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zeromq"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-libffi"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-numpy")
source=(https://github.com/ornladios/ADIOS2/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        001-fix-build-on-mingw-w64.patch
        002-fix-build-on-clangarm64.patch
        003-no-regex_h-with-clang.patch)
sha256sums=('14cf0bcd94772194bce0f2c0e74dba187965d1cffd12d45f801c32929158579e'
            '8058f2265602ad7fbf762b3f5d2a97a4efaaa9348ff97a72c017dc4302a14c27'
            '0f365062c19c21cda137ccacbf1dd33bd00afdc3418299e2abae8aab5fba33b0'
            '379fe5edf3df2474edfe6515e0592af681c17f082fb4885b5cd554bdee11cd36')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg \
    001-fix-build-on-mingw-w64.patch \
    002-fix-build-on-clangarm64.patch \
    003-no-regex_h-with-clang.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} == aarch64 ]]; then
    _extra_config+=("-DDILL_NATIVE_ONLY=OFF" "-DDILL_IGNORE_NATIVE=ON")
  fi

  CFLAGS+=" -Wno-implicit-function-declaration -Wno-int-conversion" \
  CXXFLAGS+=" -Wno-narrowing" \
  LDFLAGS+=" -Wl,--export-all-symbols" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DADIOS2_USE_EXTERNAL_DEPENDENCIES=ON \
    -DADIOS2_USE_Blosc2=ON \
    -DADIOS2_USE_BZip2=ON \
    -DADIOS2_USE_DataMan=ON \
    -DADIOS2_USE_Fortran=ON \
    -DADIOS2_USE_HDF5=ON \
    -DADIOS2_USE_Python=ON \
    -DADIOS2_USE_PNG=ON \
    -DADIOS2_USE_ZeroMQ=ON \
    -DADIOS2_USE_DataMan=OFF \
    -DADIOS2_USE_DataSpaces=OFF \
    -DADIOS2_USE_Endian_Reverse=OFF \
    -DADIOS2_USE_IME=OFF \
    -DADIOS2_USE_MGARD=OFF \
    -DADIOS2_USE_MPI=OFF \
    -DADIOS2_USE_Profiling=OFF \
    -DADIOS2_USE_SST=OFF \
    -DADIOS2_USE_SZ=OFF \
    -DADIOS2_USE_ZFP=OFF \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DADIOS2_BUILD_EXAMPLES=OFF \
    -DEVPATH_TRANSPORT_MODULES=OFF \
    -DBUILD_TESTING=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
  -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib"

  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/${_realname,,}/LICENSE
}
